<template>
  <view>
    <!-- 搜索框 -->
   <view class="searchBox">
     <!-- 搜索框组件 -->
      <YgSearch @click="gotoSearch"></YgSearch>
   </view>
    <!-- 轮播图区域 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" 
    :circular="true">
      <swiper-item v-for="(item,index) in swiperList" :key="index">
        <navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?goods_id='
        +item.goods_id">
          <image :src="item.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 分类导航区域 -->
    <view class="navList">
      <view class="navItem" v-for="(item,index) in navList" :key="index" 
      @click="navClickHandler(item)">
        <image :src="item.image_src" class="navImg"></image>
      </view>
    </view>
    <!-- 楼层区域 -->
    <view class="floorList">
      <!-- 楼层item项 -->
      <view class="floorItem" v-for="(item,index) in floorList" :key="index">
        <!-- 楼层标题 -->
        <image :src="item.floor_title.image_src" class="floorTitle"></image>
        <!-- 楼层图片区域 -->
        <view class="floorImg">
          <!-- 左边区域大图 -->
          <navigator class="left" :url="item.product_list[0].url">
            <image :src="item.product_list[0].image_src" class="leftImg"></image>
          </navigator>
          <!-- 右边区域4个小图 -->
          <view class="right">
            <navigator class="rightItem" v-for="(item2,index2) in item.product_list" :key="index2"             v-if="index2!==0" :url="item2.url">
              <image :src="item2.image_src" class="rightImg"></image>
            </navigator>
          </view>
        </view>
      </view>
    </view>

  </view>
</template>

<script>
  import YgSearch from "@/components/YgSearch/YgSearch.vue"
 import badgeMix  from '@/mixins/tabbar-badge.js'
  export default {
    mixins:[badgeMix],
    components: {
      YgSearch
    },
    data() {
      return {
        swiperList: [], //轮播图数据列表
        navList: [], //导航数据列表
        floorList: [] //楼层数据列表
      }
    },
    onLoad() {
      this.getSwiperList()
      this.getNavList()
      this.getFloorList()
    },
    methods: {
      //获取轮播图的方法
      async getSwiperList() {
        const {
          message
        } = await this.$u.get("/home/swiperdata");
        this.swiperList = message
      },
      //获取导航的方法
      async getNavList() {
        const {
          message
        } = await this.$u.get("/home/catitems")
        this.navList = message
      },
      //navItem项被点击时候
      navClickHandler(item) {
        //判断点击的是哪个nav
        if (item.name === '分类') {
          uni.switchTab({
            url: '/pages/cate/cate'
          })
        }
      },
      //获取楼层的方法
      async getFloorList() {
        const {
          message
        } = await this.$u.get("/home/floordata")
        console.log(message)
        //数据处理
        message.forEach(floor => {
          floor.product_list.forEach(prod => {
            prod.url = '/subpkg/goods_list/goods_list?' + prod.navigator_url.split('?')[1]
          })
        })
        this.floorList = message
      },
      //跳转到分包中的页面
      gotoSearch(){
        uni.navigateTo({
          url:"/subpkg/search/search"
        })
      }
    }
    
  }
</script>

<style lang="scss" scoped>
  .searchBox{
    //设置定位吸顶
    position: sticky;
    //吸顶位置
    top:0;
    //提高层级，防止被轮播图覆盖
    z-index: 99;
  }
  swiper {
    height: 340rpx;
  }

  .swiper-item,
  image {
    width: 100%;
    height: 100%;
  }

  .navList {
    display: flex;
    justify-content: space-around;
    margin: 15rpx 0;

    .navImg {
      width: 128rpx;
      height: 140rpx;
    }
  }

  .floorTitle {
    display: flex;
    height: 60rpx;
    width: 100%;
    margin-top: 30rpx;
  }

  .floorImg {
    display: flex;
    margin: 10rpx;

    .leftImg {
      width: 232rpx;
      height: 386rpx;
    }

    .right {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-around;

      .rightImg {
        width: 232rpx;
        height: 188rpx;
      }
    }
  }
</style>
